﻿using System.Collections.Generic;
using System.IO.Ports;
using System.Windows.Forms;
namespace SMSPlatform.Gateway
{

    public class SMSGateway
    {
        private SerialPort serialPort = new SerialPort();
        private int baudRate;
        private string portAddress;
        private bool connected = false;


        private List<string> operatii = new List<string>();

        


        public SMSGateway(string portAddress, int baudRate)
        {
            serialPort.DataReceived += serialPort_DataReceived;
            serialPort.ErrorReceived += serialPort_ErrorReceived;

            this.portAddress = portAddress;
            this.baudRate = baudRate;

            Timer statusCheck = new Timer();
            statusCheck.Interval = 1000;
            statusCheck.Tick += new System.EventHandler(statusCheck_Tick);
            statusCheck.Enabled = true;
           
            
        }

        void statusCheck_Tick(object sender, System.EventArgs e)
        {
            if (serialPort.IsOpen != connected)
            {
                if (StatusChanged != null) StatusChanged(connected, serialPort.IsOpen);
                connected = serialPort.IsOpen;
            }
            
        }

        public void Connect()
        {
            if (!serialPort.IsOpen)
            {

                serialPort.PortName = portAddress;

                serialPort.Open();
                serialPort.BaudRate = baudRate;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                serialPort.Parity = Parity.None;
                serialPort.Handshake = Handshake.None;

            }
            else
            {
                if (serialPort.IsOpen != connected)
                {
                    if (StatusChanged != null) StatusChanged(connected, serialPort.IsOpen);
                    connected = serialPort.IsOpen;
                }

            }
        }

        public event StatusChangedEventHandler StatusChanged;
        public delegate void StatusChangedEventHandler(bool oldValue, bool newValue);

        public event DataReceivedEventHandler DataReceived;
        public delegate void DataReceivedEventHandler(string data);


        void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            //throw new System.NotImplementedException();
        }

        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string data = serialPort.ReadExisting();
            if (DataReceived != null) DataReceived(data);
        }


        public void ExecuteATCommand(string command)
        {
            serialPort.DiscardInBuffer();

            serialPort.Write(command+"\r\n");
        }

        public void SendSMS(string number, string message)
        {
            serialPort.BaseStream.Flush();

            string cb = char.ConvertFromUtf32(26);

            System.Threading.Thread.Sleep(2000);

            serialPort.Write("AT+CMGF=1\r");
            serialPort.Write("AT+CSCA=servicecenter\r\n");

            serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");
            serialPort.Write(message + cb);
        }
    }
}
